package org.wlgzs.shop.controller;


import com.alipay.api.internal.util.StringUtils;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.UserDate;
import org.wlgzs.shop.manager.GitHubConstants;
//import org.wlgzs.shop.manager.HttpClientUtils;
import org.wlgzs.shop.manager.HttpClientUtils;
import org.wlgzs.shop.service.IUserDateService;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: Ben
 * @Date: 2019/5/22 23:43
 * @Project org.wlgzs.shop
 */
@Controller
@Api(tags = "这是第三方的接口", value = "GitHubCallBackController")
public class GitHubCallBackController {
    private static final Logger logger = LoggerFactory.getLogger(GitHubCallBackController.class);

    @Autowired
    IUserDateService userDateService;
    //授权成功后的回调,我们需要在这个方法中拿到code去请求token
    @ApiOperation(value ="第三方",httpMethod = "POST", response = ModelAndView.class)
    @RequestMapping("/callback")
    public ModelAndView callback(String code, String state, HttpSession session) throws Exception{
       ModelAndView modelAndView=new ModelAndView();
        //获取到code和state
        logger.info("code:"+code);
        logger.info("state:"+state);

        if(!StringUtils.isEmpty(code)&&!StringUtils.isEmpty(state)){
            //拿到我们的code,去请求token
            //发送一个请求到
            String token_url = GitHubConstants.TOKEN_URL.replace("CLIENT_ID", GitHubConstants.CLIENT_ID)
                    .replace("CLIENT_SECRET", GitHubConstants.CLIENT_SECRET)
                    .replace("CALLBACK", GitHubConstants.CALLBACK)
                    .replace("CODE", code);
//           System.out.println("用户信息数据"+token_url);//这个里面有我们想要的用户信息数据
            String responseStr = HttpClientUtils.doGet(token_url);
            String token = HttpClientUtils.parseResponseEntity(responseStr).get("access_token");

            //根据token发送请求获取登录人的信息
            String userinfo_url = GitHubConstants.USER_INFO_URL.replace("TOKEN", token);
            responseStr = HttpClientUtils.doGet(userinfo_url);//json

            Map<String, String> responseMap = HttpClientUtils.parseResponseEntityJSON(responseStr);
            logger.info("登录用户信息:"+responseMap);//responseMap里面保存着用户登录信息
           if (responseMap.get("login")==null){
               logger.error("登录失败");
               modelAndView.addObject("error","登录失败");
               return modelAndView;
           }
            UserDate userDate=new UserDate();
            userDate.setUserName(responseMap.get("login"));
            logger.info("获取登录用户的用户名:"+responseMap.get("login"));
            userDate.setPost(responseMap.get("email"));
            logger.info("获取登录用户的邮箱号:"+responseMap.get("email"));
            boolean judgment=userDateService.registeredInsertService(userDate);
            if (judgment){
                logger.info("注册成功！");
                session.setAttribute("user",userDate);
                modelAndView.setViewName("index");
                return modelAndView;
            }
            logger.info("登录成功！");
            session.setAttribute("user",userDate);
            modelAndView.setViewName("index");
            return modelAndView;
        }
        return modelAndView;// TODO 修改成自己需要返回的页面...
    }
}

